<template>
  <!-- ele ui 的容器分布 -->
  <el-container class="home-container">
    <!-- 头部容器 -->
    <el-header class="home-header">
      <!-- logo -->
      <div>
        <img src="~assets/img/后台管理.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <!-- 登出按钮 -->
      <el-button type="info" round @click="logout">登出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside class="home-aside" :width="asideWidth">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 一级菜单 -->
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#3f97f9" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 二级菜单 -->
          <el-submenu :index="submenu.authName" v-for="submenu in menuList" :key="submenu.id">
            <template slot="title">
              <i :class="icons[submenu.id]"></i>
              <span>{{ submenu.authName }}</span>
            </template>
            <el-menu-item :index="'/' + item.path" v-for="item in submenu.children" :key="item.id" @click="saveNavState('/' + item.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体渲染区域 -->
      <el-main class="home-mian">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "home",
  data() {
    return {
      menuList: [],
      icons: {
        125: "iconfont icon-users",
        103: "iconfont icon-quanxian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-dingdan",
        145: "iconfont icon-tongji",
      },
      isCollapse: false,
      asideWidth: "200px",
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    // 点击登出按钮登出，销毁sessionStorage并导航到login页面
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取菜单列表
    async getMenuList() {
      const { data: res } = await this.$request("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    // 点击切换侧边菜单列表的状态
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
      if (this.isCollapse) return (this.asideWidth = "64px");
      this.asideWidth = "200px";
    },
    // 保存导航栏的状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.home-header {
  background-color: #373d41;
  color: #ffffff;
  padding-left: 0;
  font-size: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  // position: fixed;

  div {
    height: 100%;
    display: flex;
    align-items: center;

    img {
      height: 60%;
      margin-left: 10px;
    }

    span {
      margin-left: 15px;
    }
  }
}

.home-aside {
  background-color: #333744;
  color: #ffffff;
  // position: fixed;
}

.home-main {
  background-color: #eaedf1;
  color: black;
}

.iconfont {
  padding-right: 10px;
}

.el-menu {
  width: 100%;
  border: none;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 45px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
